package mtn.bowen.yan.server.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import mtn.bowen.yan.server.common.Constant;
import mtn.bowen.yan.server.dao.WeekRecipeDao;
import mtn.bowen.yan.server.enums.DishesKindEnum;
import mtn.bowen.yan.server.enums.DishesTypeEnum;
import mtn.bowen.yan.server.enums.WeekEnum;
import mtn.bowen.yan.server.model.WeekRecipe;
import mtn.bowen.yan.server.service.JavaFxUiService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.*;

import static org.apache.poi.ss.usermodel.HorizontalAlignment.CENTER;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: 本周食谱
 * @date 2021-10-21 11:56 PM
 */
@Component
public class WeekRecipeController extends BaseController implements Initializable {

    private final static Logger LOGGER = LoggerFactory.getLogger(WeekRecipeController.class);

    @FXML
    private ScrollPane scroll;

    @FXML
    private VBox weekRecipeVBox;

    @Resource
    private WeekRecipeDao weekRecipeDao;

    @Resource
    private JavaFxUiService javaFxUiService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        showWeekRecipe();
    }

    private void showWeekRecipe() {

        List<WeekRecipe> weekRecipes = weekRecipeDao.selectAll();

        for (int week = 1; week <= Constant.WEEK_DAY; week++) {
            VBox weekVBox = new VBox();
            weekVBox.setPadding(new Insets(10, 10, 10, 10));
            Text weekText = javaFxUiService.getText(WeekEnum.getWeek(week).getMsg(), Font.font("KaiTi", FontWeight.NORMAL, 32));
            weekVBox.getChildren().add(weekText);
            weekVBox.setAlignment(Pos.CENTER_LEFT);
            weekVBox.setStyle("-fx-border-width: 1px;" + "-fx-border-color: -separator-color;");
            weekVBox.setSpacing(12.0);
            for (int type = 1; type <= Constant.THREE_MEALS; type++) {
                HBox hBox = new HBox();
                Text label = new Text();
                hBox.setStyle("-fx-border-width: 1px;" + "-fx-border-color: -separator-color;");
                boolean flag = true;
                for (WeekRecipe item : weekRecipes) {
                    if (item.getWeek().equals(week) && item.getType().equals(type)) {
                        if (flag) {
                            label.setText(DishesTypeEnum.getDishesType(type).getMsg());
                            label.setFont(Font.font("KaiTi", FontWeight.NORMAL, 30));
                            hBox.getChildren().add(label);
                            hBox.setAlignment(Pos.CENTER_LEFT);
                            flag = false;
                        }
                        VBox vBox = new VBox();
                        ImageView view = javaFxUiService.getImageView(80.0, 80.0, "/picture/" + item.getUuid());
                        Text text = javaFxUiService.getText(item.getName(), Font.font("KaiTi", FontWeight.NORMAL, 16));
                        vBox.getChildren().add(view);
                        vBox.getChildren().add(text);
                        vBox.setAlignment(Pos.CENTER);
                        hBox.getChildren().add(vBox);
                        //图片间距
                        hBox.setSpacing(38.0);
                    }
                }
                weekVBox.getChildren().add(hBox);
            }
            weekRecipeVBox.getChildren().add(weekVBox);
        }
    }

    @FXML
    private void exportRecipesWeek() {

        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("食谱公布表");
        // 添加表头行
        HSSFRow zeroRow = sheet.createRow(0);
        // 添加副表头
        HSSFRow oneRow = sheet.createRow(1);
        HSSFCell ce00 = zeroRow.createCell(0);
        ce00.setCellValue("食谱公布表");
        HSSFCell ce10 = oneRow.createCell(0);
        ce10.setCellValue("项目");
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(CENTER);
        ce00.setCellStyle(cellStyle);
        ce10.setCellStyle(cellStyle);
        //合并表头
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 8);
        sheet.addMergedRegion(region);
        region = new CellRangeAddress(1, 1, 0, 1);
        sheet.addMergedRegion(region);
        //早餐
        region = new CellRangeAddress(2, 9, 0, 0);
        sheet.addMergedRegion(region);
        //午餐
        region = new CellRangeAddress(10, 17, 0, 0);
        sheet.addMergedRegion(region);
        //晚餐
        region = new CellRangeAddress(18, 25, 0, 0);
        sheet.addMergedRegion(region);
        //添加星期
        for (int week = 1; week <= Constant.WEEK_DAY; week++) {
            HSSFCell weekCell = oneRow.createCell(week + 1);
            weekCell.setCellValue(WeekEnum.getWeek(week).getMsg());
            weekCell.setCellStyle(cellStyle);
        }
        //项目
        HSSFRow threeMeals = sheet.createRow(2);
        HSSFCell threeMealsCell = threeMeals.createCell(0);
        threeMealsCell.setCellValue("早餐");
        threeMealsCell.setCellStyle(cellStyle);
        threeMeals = sheet.createRow(10);
        threeMealsCell = threeMeals.createCell(0);
        threeMealsCell.setCellValue("午餐");
        threeMealsCell.setCellStyle(cellStyle);
        threeMeals = sheet.createRow(18);
        threeMealsCell = threeMeals.createCell(0);
        threeMealsCell.setCellValue("晚餐");
        threeMealsCell.setCellStyle(cellStyle);
        //添加项目说明
        for (int type = 0; type < Constant.THREE_MEALS; type++) {
            for (int kind = 1; kind <= Constant.DISHES_KIND; kind++) {
                int row = kind + 1 + type * 8;
                HSSFRow threeMeal;
                if (row == 2 || row == 10 || row == 18) {
                    threeMeal = sheet.getRow(row);
                } else {
                    threeMeal = sheet.createRow(row);
                }
                HSSFCell threeMealCell = threeMeal.createCell(1);
                threeMealCell.setCellValue(DishesKindEnum.getDishesKind(kind).getMsg());
                threeMealCell.setCellStyle(cellStyle);
            }
        }


        for (int type = 0; type < Constant.THREE_MEALS; type++) {
            for (int kind = 1; kind <= Constant.DISHES_KIND; kind++) {
                for (int week = 1; week <= Constant.WEEK_DAY; week++) {
                    List<WeekRecipe> recipes = weekRecipeDao.selectByWeekAndTypeAndKind(week, type + 1, kind);
                    if (recipes == null) {
                        continue;
                    }
                    String content = "";
                    for (WeekRecipe weekRecipe : recipes) {
                        content = content + weekRecipe.getName() + "、";
                    }
                    HSSFRow rowMonday = sheet.getRow(1 + kind + type * 8);
                    HSSFCell weekCell = rowMonday.createCell(1 + week);
                    if (content.length() > 1) {
                        content = content.substring(0, content.length() - 1);
                    }
                    weekCell.setCellValue(content);
                    weekCell.setCellStyle(cellStyle);
                }
            }
        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream("C:\\Users\\yanbowen\\Desktop\\xx\\students.xls");
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
